package com.pagoda.nerp.trade.app.goods.web;

import com.pagoda.nerp.trade.app.goods.dto.MdCategoryDto;
import com.pagoda.nerp.trade.app.goods.util.ParamValidUtil;
import com.pagoda.nerp.trade.common.BaseResult;
import com.pagoda.nerp.trade.common.GridDataResult;
import com.pagoda.nerp.trade.common.PageListResult;
import com.pagoda.nerp.trade.common.enums.ErrorCodeMappingEnums;
import com.pagoda.nerp.trade.service.MdCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 商品类别模块
 * */
@Controller
@RequestMapping("/category")
public class MdCategoryCtr {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private MdCategoryService goodsCategoryService;

	/**
	 * 查询水果分类
	 * @param params 水果分类Id
	 * @return Json
	 * */
	@RequestMapping(value = "/queryCategoryTree", method = RequestMethod.POST)
	@ResponseBody
	public BaseResult<Object> queryCategoryTree(@RequestBody Map<String,Object> params){
		logger.debug("请求 查询水果分类！水果分类Id：{} ", params);

		try {
			// 1.参数校验
			String validMsg = ParamValidUtil.queryCategoryValid(params);

			if ( null != validMsg){
				logger.error("请求参数缺失，缺失信息:{}", validMsg);
				return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS, validMsg);
			}

			// 2.查询水果分类
			PageListResult<MdCategoryDto> pageList = goodsCategoryService.queryCategoryTree(params);

			return new GridDataResult<List>(ErrorCodeMappingEnums.SUCCESS, pageList.getList(), pageList.getPageTotalCount());
		} catch (Exception e){
			logger.error("查询水果分类失败！", e);
			return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR, e.getMessage());
		}

	}

	/**
	 * 查询分类水果信息列表
	 * @param params 水果Id
	 * @return Json
	 * */
	@RequestMapping(value = "/queryCategoryGoods", method = RequestMethod.POST)
	@ResponseBody
	public BaseResult<Object> queryCategoryGoods(@RequestBody Map params){
		logger.debug("请求 查询分类水果信息列表！水果分类Id：{} ", params);

		try {
			// 1.参数校验
			String validMsg = ParamValidUtil.queryCategoryValid(params);

			if ( null != validMsg){
				logger.error("请求参数缺失，缺失信息:{}", validMsg);
				return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS, validMsg);
			}

			// 2.查询分类水果信息列表
			PageListResult<Map<String,Object>> pageList = goodsCategoryService.queryCategoryGoods(params);

			return new GridDataResult<List>(ErrorCodeMappingEnums.SUCCESS, pageList.getList(), pageList.getPageTotalCount());
		} catch (Exception e){
			logger.error("查询分类水果信息列表失败！", e.getMessage());
			return new BaseResult<Object>(ErrorCodeMappingEnums.ERROR, e.getMessage());
		}

	}
}
